package com.hskn.hss.module.tltreespec.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlemergentplan.mapper.TlEmergentPlanMapper;
import com.hskn.hss.module.tlresplan.entity.TlEmergentResplan;
import com.hskn.hss.module.tltreespec.entity.TlTreespec;
import com.hskn.hss.module.tltreespec.mapper.TlTreespecMapper;
import com.hskn.hss.module.tltreespec.service.ITlTreespecService;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-14
 */
@Slf4j
@RestController
@RequestMapping("/hss/tltreespec")
public class TlTreespecController {

    @Autowired
    ITlTreespecService tlTreespecService;
    @Resource
    TlTreespecMapper tlTreespecMapper;

    @SysLog(businessType = BusinessType.INSERT, tableName = "tl_treespec", bean = TlTreespec.class, beanMapper = TlTreespecMapper.class, names = "treespecname")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public AjaxResult<Object> insert(@RequestBody @Validated TlTreespec tlTreespec , HttpServletRequest request) {
        try {
            QueryWrapper<TlTreespec> tlTreespecQueryWrapper = new QueryWrapper<>();
            tlTreespecQueryWrapper.lambda().eq(TlTreespec::getTreespecname, tlTreespec.getTreespecname());
            List<TlTreespec> tlTreespecList = tlTreespecMapper.selectList(tlTreespecQueryWrapper);
            if (tlTreespecList.isEmpty()) {
                tlTreespecService.insert(tlTreespec);
                return AjaxResult.success(tlTreespec);
            } else {
                return AjaxResult.fail("该名称已存在，请重新输入");
            }
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "tl_treespec", idKey = "id", bean = TlTreespec.class, beanMapper = TlTreespecMapper.class, names = "treespecname")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(@RequestBody @Validated TlTreespec tlTreespec ,HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return tlTreespecService.update(tlTreespec);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "tl_treespec", idKey = "id", bean = TlTreespec.class, beanMapper = TlTreespecMapper.class, names = "treespecname")
    @RequestMapping(value = "deletebyid", method = RequestMethod.POST)
    public AjaxResult<Object> deletebyid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return tlTreespecService.deletebyid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @RequestMapping(value = "getSelectList", method = RequestMethod.POST)
    public AjaxResult<Object> getSelectList(HttpServletRequest request) {
        return AjaxResult.success(tlTreespecService.getSelectList());
    }

    @RequestMapping(value = "getDetail", method = RequestMethod.POST)
    public AjaxResult<Object> getDetail(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        TlTreespec tlTreespec = null;
        try {
            tlTreespec = tlTreespecService.getDetail(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(tlTreespec);

    }

    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public AjaxResult<Object> getList(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject json = tlTreespecService.getList(entity, params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }

    }
    @RequestMapping(value = "treespecTree", method = RequestMethod.POST)
    public AjaxResult<Object> treespecTree(HttpServletRequest request) {
        try {
            return AjaxResult.success(tlTreespecService.treespecTree());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }

    }
}

